package zhklappoint.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import zhklappoint.annotation.IgnoreAuth;
import zhklappoint.entity.WxInfo;
import zhklappoint.entity.WxName;
import zhklappoint.service.WxInfoService;
import zhklappoint.service.WxInsertInfoService;
import zhklappoint.util.ResourceUtil;
import zhklappoint.util.TurnThePage;

@Controller
public class WxInfoController {

	@Autowired
	private WxInsertInfoService wxService;
	@Autowired
	private WxInfoService wxInfoService;
	
	@IgnoreAuth
    @RequestMapping(value = "/freeWechatInfoPage",produces = "text/html;charset=UTF-8")
    public String freeWechatInfoPage(ModelMap map,Integer page,Integer size){
		String sizeconfig=ResourceUtil.getConfigByNameNeed("size");
    	page = ObjectUtils.isEmpty(page)||page==0 ? 1:page;
    	size = ObjectUtils.isEmpty(size)||size==0 ? Integer.valueOf(sizeconfig):size;
    	
		WxInfo wxinfo=new WxInfo();
		Page<WxInfo> lwinfo = wxInfoService.findAllObj(wxinfo,page,size);
		map.put("wxnameList", lwinfo);
		
		TurnThePage ttp=null;
    	if(ObjectUtils.isEmpty(lwinfo)) {
    		ttp=new TurnThePage(page,size,0,0l);
    	}else {
    		ttp=new TurnThePage(page,size,lwinfo.getTotalPages(),lwinfo.getTotalElements());
    	}
    	map.put("pageInfo", ttp);
    	return "wxindex/wxinfolist";
    }
	
	//权限列表页局部刷新
    @RequestMapping(value = "/freeSearchWechatInfoPageZone",produces = "text/html;charset=UTF-8")
    public String freeSearchWechatInfoPageZone(ModelMap map,Integer page,Integer size){
    	String sizeconfig=ResourceUtil.getConfigByNameNeed("size");
    	page = ObjectUtils.isEmpty(page)||page==0 ? 1:page;
    	size = ObjectUtils.isEmpty(size)||size==0 ? Integer.valueOf(sizeconfig):size;
    	
    	WxInfo wxinfo=new WxInfo();
		Page<WxInfo> lwinfo = wxInfoService.findAllObj(wxinfo,page,size);
		map.put("wxnameList", lwinfo);
		
		TurnThePage ttp=null;
    	if(ObjectUtils.isEmpty(lwinfo)) {
    		ttp=new TurnThePage(page,size,0,0l);
    	}else {
    		ttp=new TurnThePage(page,size,lwinfo.getTotalPages(),lwinfo.getTotalElements());
    	}
		map.put("pageInfo", ttp);
    	return "wxindex/wxinfolist::wxinfo";
    }
	
	
	@IgnoreAuth
    @RequestMapping(value = "/freeinsertWechatNamePage",produces = "text/html;charset=UTF-8")
    public String freeinsertWechatNamePage(ModelMap map){
		
		List<WxName> lwns = wxService.findAllObj();
		map.put("wxnameList", lwns);
    	return "wxindex/wxinfo";
    }
	@IgnoreAuth
	@RequestMapping(value = "/freeinsertWechatName",produces = "text/html;charset=UTF-8")
	@ResponseBody
    public void insertWechatName(String name){
		if(ObjectUtils.isEmpty(name)) {
			return;
		}
		WxName pwns = wxService.findByName(name);
		if(!ObjectUtils.isEmpty(pwns)) {//已有相同公众号
			return;
		}
		WxName wxname=new WxName();
		wxname.setWxName(name);
		wxService.insertIntoWxName(wxname);
	}
	
}
